﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
namespace NCKH.Admin
{
    public partial class danhmuctin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txttimkiem.Attributes.Add("PlaceHolder", "Nhập tên danh mục cần tìm...");
            if (!IsPostBack)
            {
                hiendanhmuctin();
                hientennhomtin();
            }
        }

        protected void hientennhomtin()
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlselect = "SELECT Manhomtin, Tennhomtin FROM NHOMTIN";
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand(sqlselect, cnn))
                {
                    cnn.Open();
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        if (rd.HasRows)
                            while (rd.Read())
                            {
                                drdtennhom.Items.Add(
                                    new ListItem(rd["Tennhomtin"].ToString(), rd["Manhomtin"].ToString()));
                            }
                        else
                            btnthem.Enabled = false;
                        rd.Close();
                    }
                    cnn.Close();
                }
            }
        }

        protected void hiendanhmuctin()
        {
            using (DataTable tb = getdanhmuctin())
            {
                grvnhomtin.DataSource = tb;
                grvnhomtin.DataBind();

            }
        }
        protected DataTable getdanhmuctin()
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand("select_danhmuc", cnn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        DataTable tbl = new DataTable();
                        da.Fill(tbl);
                        return tbl;

                    }
                }
            }
        }
        protected bool kiemtraten()
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string ktten = "SELECT Tendanhmuctin From DANHMUCTIN WHERE Tendanhmuctin=N'" + txttendanhmuc.Text + "'";
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand(ktten, cnn))
                {
                    cmd.CommandType = CommandType.Text;
                    cnn.Open();
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        if (rd.HasRows && rd.Read())
                        {
                            // Response.Write("<script>alert('Tên đã bị trùng')</script>");
                            lbtb.Text = "Tên danh mục đã bị trùng";
                            return true;
                        }
                        else
                        {

                            lbtb.Text = "";
                            return false;
                        }
                        rd.Close();
                    }

                }
                cnn.Close();
            }
        }



        protected void grvnhomtin_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower().Equals("xoa1"))
            {
                int madanhmuc = Convert.ToInt32(e.CommandArgument);
                string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
                string delete_danhmuc = "DELETE FROM DANHMUCTIN WHERE Madanhmuctin='" + madanhmuc + "'";
                using (SqlConnection cnn = new SqlConnection(connectionstring))
                {
                    using (SqlCommand cmd = new SqlCommand(delete_danhmuc, cnn))
                    {
                        cmd.CommandType = CommandType.Text;
                        cnn.Open();
                        cmd.ExecuteNonQuery();
                        cnn.Close();
                        hiendanhmuctin();
                    }
                }
            }
        }


        protected void grvnhomtin_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvnhomtin.PageIndex = e.NewPageIndex;
            this.hiendanhmuctin();

        }

        protected void grvnhomtin_DataBound(object sender, EventArgs e)
        {

        }

        protected void btnthem_Click(object sender, EventArgs e)
        {

            if (kiemtraten() == false)
                themdanhmuctin();
        }


        protected void themdanhmuctin()
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand("themdanhmuctin", cnn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("Tendanhmuctin", txttendanhmuc.Text);
                    cmd.Parameters.AddWithValue("Manhomtin", drdtennhom.SelectedValue);
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                    cnn.Close();
                    txttendanhmuc.Text = string.Empty;
                    txttendanhmuc.Focus();
                    hiendanhmuctin();
                }
            }

        }


        protected void tbnxoa_Click(object sender, EventArgs e)
        {

        }

        protected void btnhuy_Click(object sender, EventArgs e)
        {
            txttendanhmuc.Text = string.Empty;
            txttendanhmuc.Focus();
        }



        protected void btntimkiem_Click(object sender, EventArgs e)
        {
            int catID = Convert.ToInt32(drdtennhom.SelectedValue);
            using (DataTable tb = hientimkiemdanhmuc(catID))
            {
                grvnhomtin.DataSource = tb;
                grvnhomtin.DataBind();
            }
        }


        protected DataTable hientimkiemdanhmuc(int catID)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            //string timkiemdanhmuc = "SELECT Madanhmuctin, Tennhomtin, Tendanhmuctin"
            //    + "FROM DANHMUCTIN, NHOMTIN"
            //    + "WHERE DANHMUCTIN.Manhomtin=NHOMTIN.Manhomtin and Tendanhmuc='"+txttendanhmuc.Value +"' and Manhomtin='" + catID + "'";
            using (SqlConnection cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand("timkiemdanhmuctin", cnn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Tendanhmuctin", '%' + txttimkiem.Text + '%');
                    cmd.Parameters.AddWithValue("@Manhomtin", catID);
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        da.SelectCommand = cmd;
                        DataTable tbl = new DataTable();
                        da.Fill(tbl);
                        return tbl;
                    }
                }
            }
        }

        protected void grvnhomtin_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}